package com.longway.core.ext.excel.data;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.longway.core.ext.excel.error.ErrorException;


public class RowsetData
{
    private List rowData;
    private TableMeta tableMeta;

    public List getRowData()
    {
        return rowData;
    }

    public void setRowData(List rowData)
    {
        this.rowData = rowData;
    }

    public RowsetData(TableMeta tableMeta, List rowData)
    {
        this.tableMeta = tableMeta;
        this.rowData = rowData;
    }

    public TableMeta getTableMeta()
    {
        return tableMeta;
    }

    public String getTableName()
    {
        if (tableMeta != null)
        {
            return tableMeta.getTable();
        }

        return null;
    }
    
    public ColumnMeta getColumn(int i)
    {
        if (i < tableMeta.getColumns().size())
        {
            return tableMeta.getColumns().get(i);
        }
        return null;
    }
    
    public ColumnMeta getColumn(String columnName)
    {
    	List<ColumnMeta> columns = tableMeta.getColumns();
    	for(ColumnMeta column : columns){
    		if(columnName.equals(column.getColumn())){
    			return column;
    		}
    	}
        return null;
    }

    public int getColumnSize()
    {
        return rowData.size();
    }

    public Object[] toObject()
    {

        List<ColumnMeta> meta = tableMeta.getColumns();
        if (meta.size() > 0)
        {
            Object[] values = new Object[meta.size()];
            for (int i = 0; i < meta.size(); i++)
            {
                Integer pos = tableMeta.getColumnKeys().get(meta.get(i).getColumn());
                if (pos != null && pos < rowData.size())
                {
                    values[i] = rowData.get(pos);
                }

            }
            return values;
        }

        return null;
    }

    public String getString(int i) throws ErrorException
    {
        if (i < rowData.size())
        {
            Object value = rowData.get(i);
            if (value == null)
            {
                return null;
            }
            ColumnMeta column = tableMeta.getColumn(i);
            try
            {
                if (column.getDataTypeName().equalsIgnoreCase("varchar"))
                {
                	//(String)value
                    return String.valueOf(value);
                }
                if (column.getDataTypeName().equalsIgnoreCase("int")||column.getDataTypeName().equalsIgnoreCase("bigint"))
                {
                    return String.valueOf(value);
                }

                if (column.getDataTypeName().equalsIgnoreCase("Date"))
                {
                	 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                     if ((Date) value != null)
                     {
                         return formatter.format((Date) value);
                     }
                     else
                     {
                         return formatter.format(new Date());
                     }
                }
                if (column.getDataTypeName().equalsIgnoreCase("DateTime"))
                {
                	 SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
                     if ((Date) value != null)
                     {
                         return formatter.format((Date) value);
                     }
                     else
                     {
                         return "";
                     }
                }
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
            try
            {
                return String.valueOf(value);
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
        }
        throw new ErrorException(50);
    }

    public boolean hasColumn(String name)
    {
        Integer pos = tableMeta.getColumnKeys().get(name);
        if (pos != null)
        {
            return true;
        }

        return false;
    }

    public String getFieldString(String name) throws ErrorException
    {
        if (name == null)
        {
            throw new ErrorException(50);
        }
        Integer pos = tableMeta.getColumnKeys().get(name);
        if (pos != null && pos < rowData.size())
        {
            return getString(pos);
        }

        throw new ErrorException(50);
    }

  
    public Integer getInteger(int i) throws ErrorException
    {
        if (i < rowData.size())
        {
            Object value = rowData.get(i);
            if (value == null)
            {
                return null;
            }
            ColumnMeta column = tableMeta.getColumn(i);
            try
            {
                if (column.getDataTypeName().equalsIgnoreCase("varchar"))

                {
                    return Integer.valueOf((String) value);
                }
                if (column.getDataTypeName().equalsIgnoreCase("int"))
                {
                    return (Integer) value;
                }
                if (column.getDataTypeName().equalsIgnoreCase("Date"))
                {
                    throw new ErrorException(310);
                }
                if (column.getDataTypeName().equalsIgnoreCase("DateTime"))
                {
                    throw new ErrorException(311);
                }
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
            try
            {
                return Integer.valueOf(String.valueOf(value));
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
        }
        throw new ErrorException(50);

    }

    public Integer getFieldInteger(String name) throws ErrorException
    {
        if (name == null)
        {
            throw new ErrorException(50);
        }
        Integer pos = tableMeta.getColumnKeys().get(name);
        if (pos < rowData.size())
        {
            return getInteger(pos);
        }

        throw new ErrorException(50);
    }

  
    public Long getLong(int i) throws ErrorException
    {
        if (i < rowData.size())
        {
            Object value = rowData.get(i);
            if (value == null)
            {
                return null;
            }
            ColumnMeta column = tableMeta.getColumn(i);
            try
            {
                if (column.getDataTypeName().equalsIgnoreCase("varchar"))

                {
                    return Long.valueOf((String) value);
                }
                if (column.getDataTypeName().equalsIgnoreCase("int")||column.getDataTypeName().equalsIgnoreCase("bigint"))
                {
                    return (Long) value;
                }
                if (column.getDataTypeName().equalsIgnoreCase("Date"))
                {
                    throw new ErrorException(310);
                }
                if (column.getDataTypeName().equalsIgnoreCase("DateTime"))
                {
                    throw new ErrorException(311);
                }
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
            try
            {
                return Long.valueOf((String) value);
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
        }
        throw new ErrorException(50);

    }

    public Long getFieldLong(String name) throws ErrorException
    {
        if (name == null)
        {
            throw new ErrorException(50);
        }
        Integer pos = tableMeta.getColumnKeys().get(name);
        if (pos < rowData.size())
        {
            return getLong(pos);
        }

        throw new ErrorException(50);
    }

   
    public Date getDate(int i) throws ErrorException
    {
        if (i < rowData.size())
        {
            Object value = rowData.get(i);
            if (value == null)
            {
                return null;
            }
            ColumnMeta column = tableMeta.getColumn(i);
            try
            {
                if (column.getDataTypeName().equalsIgnoreCase("varchar"))
                {
                	  SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss", Locale.US);
                      Date new_date = null;
                      if ((String) value != null)
                      {
                          if (!((String) value).equals(""))
                          {
                              try
                              {
                                  new_date = formatter.parse((String) value);
                              }
                              catch (ParseException e)
                              {
                                  e.printStackTrace();
                              }
                          }
                      }
                      return new_date;
                }
                if (column.getDataTypeName().equalsIgnoreCase("Integer"))
                {
                    throw new ErrorException(320);
                }
                if (column.getDataTypeName().equalsIgnoreCase("Date"))
                {
                    return (Date) value;
                }
                if (column.getDataTypeName().equalsIgnoreCase("DateTime"))
                {
                    if (value instanceof Timestamp)
                    {
                        return (Timestamp) value;
                    }
                    return new Date((String) value);
                }
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
            try
            {
                return new Date((String) value);
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
        }
        throw new ErrorException(50);
    }

    public Date getFieldDate(String name) throws ErrorException
    {
        if (name == null)
        {
            throw new ErrorException(50);
        }
        Integer pos = tableMeta.getColumnKeys().get(name);
        if (pos < rowData.size())
        {
            return getDate(pos);
        }

        throw new ErrorException(50);
    }

   
    public Double getDouble(int i) throws ErrorException
    {
        if (i < rowData.size())
        {
            Object value = rowData.get(i);
            if (value == null)
            {
                return null;
            }
            ColumnMeta column = tableMeta.getColumn(i);
            try
            {
                if (column.getDataTypeName().equalsIgnoreCase("varchar"))
                {
                    return Double.valueOf((String) value);
                }
                if (column.getDataTypeName().equalsIgnoreCase("decimal"))
                {
                	BigDecimal bg = (BigDecimal)value;
                    return bg.doubleValue();
                }
                if (column.getDataTypeName().equalsIgnoreCase("Date"))
                {
                    throw new ErrorException(330);
                }
                if (column.getDataTypeName().equalsIgnoreCase("DateTime"))
                {
                    throw new ErrorException(331);
                }
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
            try
            {
                return Double.valueOf((String) value);
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
        }
        throw new ErrorException(50);
    }

    public Double getFieldDouble(String name) throws ErrorException
    {
        if (name == null)
        {
            throw new ErrorException(50);
        }
        Integer pos = tableMeta.getColumnKeys().get(name);
        if (pos != null && pos < rowData.size())
        {
            return getDouble(pos);
        }

        throw new ErrorException(50);
    }


    public BigDecimal getBigDecimal(int i) throws ErrorException
    {
        if (i < rowData.size())
        {
            Object value = rowData.get(i);
            if (value == null)
            {
                return null;
            }
            ColumnMeta column = tableMeta.getColumn(i);
            try
            {
                if (column.getDataTypeName().equalsIgnoreCase("decimal"))
                {
                    return BigDecimal.valueOf((Double)value);
                }
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
            try
            {
                return BigDecimal.valueOf((Double) value);
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
        }
        throw new ErrorException(50);
    }

    public BigDecimal getFieldBigDecimal(String name) throws ErrorException
    {
        if (name == null)
        {
            throw new ErrorException(50);
        }
        Integer pos = tableMeta.getColumnKeys().get(name);
        if (pos != null && pos < rowData.size())
        {
            return getBigDecimal(pos);
        }

        throw new ErrorException(50);
    }
   
    public Object getObject(int i) throws ErrorException
    {
        if (i < rowData.size())
        {
            return rowData.get(i);
        }
        return null;
    }

    public Object getObject(String name) throws ErrorException
    {
        if (name == null)
        {
            throw new ErrorException(50);
        }
        Integer pos = tableMeta.getColumnKeys().get(name);
        if (pos < rowData.size())
        {
            return getObject(pos);
        }

        throw new ErrorException(50);
    }

   
    public Float getFloat(int i) throws ErrorException
    {
        if (i < rowData.size())
        {
            Object value = rowData.get(i);
            if (value == null)
            {
                return null;
            }
            ColumnMeta column = tableMeta.getColumn(i);
            try
            {
                if (column.getDataTypeName().equalsIgnoreCase("Varchar2"))
                {
                    return Float.valueOf((String) value);
                }
                if (column.getDataTypeName().equalsIgnoreCase("NUMBER"))
                {
                    return Float.valueOf(String.valueOf((Integer) value));
                }
                if (column.getDataTypeName().equalsIgnoreCase("Date"))
                {
                    throw new ErrorException(330);
                }
                if (column.getDataTypeName().equalsIgnoreCase("DateTime"))
                {
                    throw new ErrorException(331);
                }
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
            try
            {
                return Float.valueOf((String) value);
            }
            catch (Exception ex)
            {
                throw new ErrorException(300, ex.fillInStackTrace());
            }
        }
        throw new ErrorException(50);
    }

    public Float getFieldFloat(String name) throws ErrorException
    {
        if (name == null)
        {
            throw new ErrorException(50);
        }
        Integer pos = tableMeta.getColumnKeys().get(name);
        if (pos < rowData.size())
        {
            return getFloat(pos);
        }

        throw new ErrorException(50);
    }
}
  
